#!/usr/bin/python3
# -*- coding: utf8 -*-
# Date   : 2021/06/26
# Author : Kwan
# Email  : allenxian@live.com
# Blog   : https://my.oschina.net/u/4173440

import configparser
from functools import wraps
from flask import abort, jsonify
from flask_login import current_user



class PermissionsModule:
    # 用户
    user = 1
    # 管理员
    manager = 2
    # 系统管理员
    admin = 4

    default_roles = [
        {
            'Name': 'user',
            'Description': '用戶',
            'Permissions': [user, ]
        },
        {
            'Name': 'manager',
            'Description': '管理員',
            'Permissions': [user, manager]
        },
        {
            'Name': 'admin',
            'Description': '系統管理員',
            'Permissions': [user, manager, admin]
        }
    ]

def permission_required(permission):
    """
    权限检验
    """
    def decorator(f):
        @wraps(f)
        def decorated_function(*args, **kwargs):
            if not current_user.can(permission):
                # 抛出403
                abort(403)
            return f(*args, **kwargs)
        return decorated_function
    return decorator

def manager_required(f):
    """
    管理员检查
    """
    return permission_required(PermissionsModule.manager)(f)

def admin_required(f):
    """
    管理员检查
    """
    return permission_required(PermissionsModule.admin)(f)


class CarParkConfig(configparser.ConfigParser):
    def __init_subclass__(cls) -> None:
        return super().__init_subclass__()
    
    def optionxform(self, optionstr: str) -> str:
        return optionstr


class Struct(object):
    """
    公共对象
    """
    def __init__(self, **entries) -> None:
        self.__dict__.update(**entries)


class AxiosMessage:
    """
    应用消息
    """ 

    def __init__(self) -> None:
        pass

    def success(self, msg):
        return jsonify(**{'icon': 'success', 'text': msg})
    
    def warning(self, msg):
        return jsonify(**{'icon': 'warning', 'text': msg})

    def error(self, msg):
        return jsonify(**{'icon': 'error', 'text': msg})

    def json_data(self, **data):
        return jsonify(**data)


class DataClasses:
    """
    数据类型
    """
    Define_GateTypes = [{"id": 1, "name": "入口"}]




